import 'package:flutter/material.dart';
import '../../res.dart';
import '../../utils/color/ColorUtils.dart';
import '../../widgets/MyLocalImage.dart';
import '../../widgets/TextView.dart';
import 'package:get/get.dart';
import 'package:share_plus/share_plus.dart';

import '../../config/Constant.dart';
import '../../model/Bus.dart';
import '../../model/Img.dart';
import '../../model/PDFFile.dart';
import '../../utils/EventBus/EventBus.dart';
import '../convert/FileInfo.dart';

class SetDialog extends StatefulWidget {
  SetDialog({Key? key, required this.pdfFile}) : super(key: key);
  PDFFile pdfFile;

  @override
  State<SetDialog> createState() => _SortDialogState();
}

class _SortDialogState extends State<SetDialog> {
  List<Img> list = [];
  TextEditingController controller = TextEditingController();
  FocusNode text = FocusNode();
  String name = "";

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    text.requestFocus();
    list.add(Img("share".tr, Res.snackbar_icon_share, 0));
    if (widget.pdfFile.star) {
      list.add(Img("unstar".tr, Res.snackbar_icon_addstar, 1));
    } else {
      list.add(Img("Add_star".tr, Res.snackbar_icon_unstar, 2));
    }
    list.add(Img("rename".tr, Res.snackbar_icon_rename, 3));
    list.add(Img("delete_files".tr, Res.snackbar_icon_delect1, 4));
    list.add(Img("file_informatio".tr, Res.snackbar_icon_inf, 5));

    controller.value = controller.value.copyWith(
      text: widget.pdfFile.name.substring(0, widget.pdfFile.name.length - 4),
      composing: TextRange.empty,
    );
  }

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 400,
      padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 20),
      decoration: BoxDecoration(
          color: Colors.white, borderRadius: BorderRadius.circular(12)),
      child: Column(
        mainAxisSize: MainAxisSize.min,
        children: [
          Container(
            margin: const EdgeInsets.only(bottom: 10),
            child: Row(
              children: [
                Expanded(
                  child: TextView(widget.pdfFile.name,
                      style: TextStyle(
                          color: ColorUtils.text,
                          fontSize: 18,
                          fontWeight: FontWeight.bold)),
                ),
                MyLocalImage(
                  Res.sys_icon_delete_grey3x,
                  width: 20,
                  height: 20,
                  onclick: () {
                    Get.back();
                  },
                ),
              ],
            ),
          ),
          const Divider(),
          Expanded(
              child: ListView.builder(
            itemBuilder: (c, index) {
              return InkWell(
                onTap: () {
                  switch (list[index].label) {
                    case 0:
                      Get.back();
                      final files = <XFile>[];
                      files.add(XFile(widget.pdfFile.path));
                      Share.shareXFiles(files, text: widget.pdfFile.name);
                      break;
                    case 1:
                      Get.back();
                      EventBusUtil.fireEvent(
                          Bus(Constant.STAR_MSG_N, widget.pdfFile));
                      break;
                    case 2:
                      Get.back();
                      EventBusUtil.fireEvent(
                          Bus(Constant.STAR_MSG_Y, widget.pdfFile));
                      break;
                    case 3:
                      Get.back();
                      Get.defaultDialog(
                        radius: 10,
                        title: "rename".tr,
                        contentPadding: EdgeInsets.zero,
                        titlePadding: const EdgeInsets.only(top: 20),
                        content: Container(
                          width: MediaQuery.of(context).size.width,
                          height: 44,
                          margin: const EdgeInsets.symmetric(
                              horizontal: 32, vertical: 20),
                          padding: const EdgeInsets.only(left: 10),
                          decoration: BoxDecoration(
                              borderRadius: BorderRadius.circular(6),
                              color: rgba(247, 248, 250, 1)),
                          child: TextField(
                            autofocus: true,
                            controller: controller,
                            onChanged: (v) {
                              name = v;
                            },
                            decoration: InputDecoration(
                              suffixIcon: Padding(
                                padding: const EdgeInsets.all(14),
                                child: Offstage(
                                  offstage: name.isEmpty ? true : false,
                                  child: InkWell(
                                    onTap: () {
                                      controller.clear();
                                      name = "";
                                      // setState(() {});
                                    },
                                    child: const Image(
                                      width: 16,
                                      height: 16,
                                      image: AssetImage(
                                          Res.sys_icon_delete_grey3x),
                                    ),
                                  ),
                                ),
                              ),
                              border: InputBorder.none,
                              hintText: 'enter_file_name'.tr,
                              hintStyle:
                                  TextStyle(color: rgba(200, 202, 204, 1)),
                            ),
                          ),
                        ),
                        actions: [
                          Column(
                            children: [
                              const Divider(
                                height: 1,
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  TextView(
                                    "cancel".tr,
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: Colors.black,
                                        fontSize: 16),
                                    backcolor: rgba(247, 247, 247, 1),
                                    textAlign: TextAlign.center,
                                    radius: BorderRadius.circular(6),
                                    padding: const EdgeInsets.symmetric(
                                        vertical: 11),
                                    width: 140,
                                    margin: const EdgeInsets.symmetric(
                                        vertical: 16),
                                    onclick: () {
                                      Get.back();
                                    },
                                  ),
                                  const SizedBox(
                                    width: 16,
                                  ),
                                  TextView(
                                    "ok".tr,
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: Colors.white,
                                        fontSize: 16),
                                    backcolor: rgba(255, 78, 77, 1),
                                    textAlign: TextAlign.center,
                                    radius: BorderRadius.circular(6),
                                    padding: const EdgeInsets.symmetric(
                                        vertical: 11),
                                    width: 140,
                                    margin: const EdgeInsets.symmetric(
                                        vertical: 16),
                                    onclick: () {
                                      Get.back();
                                      widget.pdfFile.name = "$name.pdf";
                                      EventBusUtil.fireEvent(Bus(
                                          Constant.CONVERT_EDIT_MSG,
                                          widget.pdfFile));
                                    },
                                  ),
                                ],
                              ),
                            ],
                          )
                        ],
                      );
                      break;
                    case 4:
                      Get.back();
                      Get.defaultDialog(
                        radius: 10,
                        contentPadding: EdgeInsets.zero,
                        titlePadding: const EdgeInsets.only(top: 20),
                        content: Container(
                          alignment: Alignment.center,
                          width: 400,
                          child: TextView(
                            "delete_file".tr,
                            maxLines: 2,
                            textAlign: TextAlign.center,
                            style: TextStyle(
                                color: ColorUtils.text,
                                fontSize: 16,
                                fontWeight: FontWeight.bold),
                            margin: const EdgeInsets.symmetric(
                                horizontal: 32, vertical: 16),
                          ),
                        ),
                        actions: [
                          Column(
                            children: [
                              const Divider(
                                height: 1,
                              ),
                              Row(
                                mainAxisAlignment: MainAxisAlignment.center,
                                crossAxisAlignment: CrossAxisAlignment.center,
                                children: [
                                  TextView(
                                    "cancel".tr,
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: Colors.black,
                                        fontSize: 16),
                                    backcolor: rgba(247, 247, 247, 1),
                                    textAlign: TextAlign.center,
                                    radius: BorderRadius.circular(6),
                                    padding: const EdgeInsets.symmetric(
                                        vertical: 11),
                                    width: 140,
                                    margin: const EdgeInsets.symmetric(
                                        vertical: 16),
                                    onclick: () {
                                      Get.back();
                                    },
                                  ),
                                  const SizedBox(
                                    width: 16,
                                  ),
                                  TextView(
                                    "delete".tr,
                                    style: const TextStyle(
                                        fontWeight: FontWeight.bold,
                                        color: Colors.white,
                                        fontSize: 16),
                                    backcolor: rgba(255, 78, 77, 1),
                                    textAlign: TextAlign.center,
                                    radius: BorderRadius.circular(6),
                                    padding: const EdgeInsets.symmetric(
                                        vertical: 11),
                                    width: 140,
                                    margin: const EdgeInsets.symmetric(
                                        vertical: 16),
                                    onclick: () {
                                      Get.back();
                                      EventBusUtil.fireEvent(Bus(
                                          Constant.DELETE_MSG, widget.pdfFile));
                                    },
                                  ),
                                ],
                              ),
                            ],
                          )
                        ],
                      );
                      break;
                    case 5:
                      Get.to(FileInfo(
                        pdfFile: widget.pdfFile,
                      ));
                      break;
                    case 6:
                      break;
                    case 7:
                      break;
                    default:
                      print('UNKNOW');
                  }
                },
                child: Container(
                  height: 60,
                  child: Row(
                    children: [
                      MyLocalImage(
                        list[index].icon,
                        width: 24,
                        height: 24,
                      ),
                      TextView(
                        list[index].title,
                        margin: EdgeInsets.only(left: 12),
                      )
                    ],
                  ),
                ),
              );
            },
            itemCount: list.length,
          )),
        ],
      ),
    );
  }
}
